package github.leavesczy.reactivehttpsamples.widget.dialog;

import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import github.leavesczy.reactivehttpsamples.R;
import github.leavesczy.reactivehttpsamples.widget.dialog.base.BaseDialogFragment;
import github.leavesczy.reactivehttpsamples.widget.dialog.base.DialogLayoutCallback;


public class BridgeFileNameDialog extends BaseDialogFragment {


    private EditText etBridgeFileName;
    private TextView       tvOk;
    private TextView       tvCancel;

    public BridgeFileNameDialog init(Context context, final CharSequence defaultFileName,
                                     Callback callback) {
        super.init(context, new DialogLayoutCallback() {
            @Override
            public int bindTheme() {
                return R.style.CommonContentDialogStyle;
            }

            @Override
            public int bindLayout() {
                return R.layout.dialog_bridge_file_name;
            }

            @Override
            public void initView(final BaseDialogFragment dialog, View contentView) {
                etBridgeFileName = contentView.findViewById(R.id.etBridgeFileName);


                tvOk = contentView.findViewById(R.id.tvOk);
                tvCancel = contentView.findViewById(R.id.tvCancel);

                etBridgeFileName.setText(defaultFileName);

                ClickUtils.applyPressedBgDark(tvOk);
                ClickUtils.applyPressedBgDark(tvCancel);
                tvOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String fileName = etBridgeFileName.getText().toString();
                        if(StringUtils.isEmpty(fileName)){
                            ToastUtils.showShort("文件名不能为空");
                            return;
                        }
                        if(fileName.contains(".")){
                            int lastIndexOf = fileName.lastIndexOf(".");
                            String substring = fileName.substring(lastIndexOf, fileName.length());
                            if(!StringUtils.equals(substring,".csv")){
                                ToastUtils.showShort("文件格式不是 csv格式，请确认");
                                return;
                            }
                        }else {
                            fileName=fileName+".csv";
                        }
                        dismiss();
                        callback.ok(fileName);
                    }
                });

                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        callback.cancel();
                    }
                });
            }

            @Override
            public void setWindowStyle(Window window) {
            }

            @Override
            public void onCancel(BaseDialogFragment dialog) {

            }

            @Override
            public void onDismiss(BaseDialogFragment dialog) {

            }
        });
        return this;
    }

    public interface Callback{
        void ok(String fileName);
        void cancel();
    }
}